package kt.com.bilin.coin.utils;

import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;

public class ViewUtils {
    public static <T extends View> T findViewByClass(View rootView, Class<T> clz) {
        LinkedList<View> views = new LinkedList<>();
        views.push(rootView);
        while (!views.isEmpty()) {
            View popView = views.pop();
            if (popView instanceof ViewGroup) {
                ViewGroup view = (ViewGroup) popView;
                for (int i = 0; i < view.getChildCount(); i++) {
                    View child = view.getChildAt(i);
                    if (child.getClass().isAssignableFrom(clz))
                        return (T) child;
                    if (child instanceof ViewGroup)
                        views.push(child);
                }
            }
        }
        return null;
    }

    public static int getStatusBarHeight(Resources resources) {
        return resources.getDimensionPixelSize(resources.getIdentifier("status_bar_height", "dimen", "android"));
    }
}
